#include "smallsatos.h"
#include "dconfig.h"
#include "rtc.h"
#include "int_eeprom.h"
#include "storage.h"

INT8U timer_emission;
INT8U timer_temperature;
INT16U timer_dose;
INT16U timer_mesure;
INT8U timer_puissance;

INT8U emission_enabled;
INT8U experience_enabled;
INT8U puissance_enabled;

void loadTimers(void)
{
	timer_emission = EERead(EE_TIMER_EMISSION);
	timer_temperature = EERead(EE_TIMER_TEMPERATURE);
	timer_dose = EERead(EE_TIMER_DOSE) + (EERead(EE_TIMER_DOSE + 1) * 256);
	timer_mesure = EERead(EE_TIMER_MESURE) + (EERead(EE_TIMER_MESURE + 1) * 256);
	timer_puissance = EERead(EE_TIMER_PUISSANCE);
}

void reconfig (INT8U id, INT16U value)
{
	INT16U err;
	eeprom_query query;
	INT8U value8 = value & 0xFF;
	switch (id)
	{
		case 0x3:
			/* Ping */
			break;
		
		case 0x5:
			/* Reset */
			_asm
				RESET
			_endasm
			break;

		case 0x6:
			/* Config RTC Annee */
			pcf8563_put(RTC_ANNEES, value8);
			break;

		case 0x9:
			/* Config RTC Mois */
			pcf8563_put(RTC_MOIS, value8);
			break;

		case 0xA:
			/* Config RTC Jour */
			pcf8563_put(RTC_JOURS, value8);
			break;

		case 0xC:
			/* Config RTC Heure */
			pcf8563_put(RTC_HEURES, value8);
			break;

		case 0xF:
			/* Config RTC Minutes */
			pcf8563_put(RTC_MINUTES, value8);
			break;

		case 0x11:
			/* Config RTC Secondes */
			pcf8563_put(RTC_SECONDES, value8);
			break;

		case 0x14:
			/* Activer/desactiver l'emission */
			break;

		case 0x17:
			/* Activer/desactiver l'experience */
			break;

		case 0x18:
			/* Activer/desactiver l'interrogation de la carte puissance */
			break;

		case 0x1B:
			/* Modifier timer emission */
			query.type = WRITE_QUERY;
			query.size = 1;
			query.address = EE_TIMER_EMISSION;
			query.value = &value8;

			OSSemPend(get_eeprom_lock(), 0, &err);
			OSMboxPost(get_eeprom_query_mbox(), &query);
			OSSemPost(get_eeprom_lock());

			timer_emission = value8;
			break;					

		case 0x1D:
			/* Modifier timer mesure temperature */
			query.type = WRITE_QUERY;
			query.size = 1;
			query.address = EE_TIMER_TEMPERATURE;
			query.value = &value8;

			OSSemPend(get_eeprom_lock(), 0, &err);
			OSMboxPost(get_eeprom_query_mbox(), &query);
			OSSemPost(get_eeprom_lock());

			timer_temperature = value8;
			break;

		case 0x1E:
			/* Modifier timer mesure dose */
			query.type = WRITE_QUERY;
			query.size = 2;
			query.address = EE_TIMER_DOSE;
			query.value = (INT8U*) &value;

			OSSemPend(get_eeprom_lock(), 0, &err);
			OSMboxPost(get_eeprom_query_mbox(), &query);
			OSSemPost(get_eeprom_lock());

			timer_dose = value;
			break;

		case 0x21:
			/* Modifier timer experience */
			query.type = WRITE_QUERY;
			query.size = 2;
			query.address = EE_TIMER_MESURE;
			query.value = (INT8U*) &value;

			OSSemPend(get_eeprom_lock(), 0, &err);
			OSMboxPost(get_eeprom_query_mbox(), &query);
			OSSemPost(get_eeprom_lock());

			timer_mesure = value;
			break;

		case 0x22:
			/* Modifier timer puissance */
			query.type = WRITE_QUERY;
			query.size = 2;
			query.address = EE_TIMER_PUISSANCE;
			query.value = (INT8U*) &value;

			OSSemPend(get_eeprom_lock(), 0, &err);
			OSMboxPost(get_eeprom_query_mbox(), &query);
			OSSemPost(get_eeprom_lock());

			timer_puissance = value8;
			break;

		case 0x24:
			/* Modifier la priorite d'un parametre dans le journal */
			query.type = WRITE_QUERY;
			query.size = 1;
			query.address = EE_PRIORITES_JOURNAL + (value >> 1);
			query.value = (INT8U*) (&value + 1);

			OSSemPend(get_eeprom_lock(), 0, &err);
			OSMboxPost(get_eeprom_query_mbox(), &query);
			OSSemPost(get_eeprom_lock());
			break;

		case 0x2D:
			/* Modifier le gain OSL */
			break;

		case 0x2E:
			/* Modifier distri Exp1 */
			break;

		case 0x30:
			/* Modifier distri Int1 */
			break;

		case 0x33:
			/* Modifier distri Exp2 */
			break;

		case 0x35:
			/* Modifier distri Int2 */
			break;

		case 0x36:
			/* Modifier distri OSL */
			break;

		case 0xCC:
			/* EEPROM wipe */
			query.type = WIPE_QUERY;
			OSSemPend(get_eeprom_lock(), 0, &err);
			OSMboxPost(get_eeprom_query_mbox(), &query);
			OSSemPost(get_eeprom_lock());
			break;
	}
}